package com.study.servlet.request;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.study.servlet.domain.Test;

/**
* @Name: GetRequestBodyInfo
* @Description: 使用Request对象获取页面请求的数据
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-3-1 下午4:22:12 
* @Version: V1.0
 */
public class GetRequestBodyInfo extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			setRequestParams2FormBeanByBeanUtils(request, response) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	* @Name: getRequestBodyInfo01
	* @Description: 根据请求参数名称获取参数内容
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 下午4:35:27
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Return: void
	 */
	private void getRequestBodyInfo01(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8") ;
		PrintWriter out = response.getWriter() ;
		String testname = request.getParameter("testname") ;
		out.print("testname:" + testname + "<br/>") ;
		String[] password = request.getParameterValues("password") ;
		out.print("password:" + Arrays.toString(password) + "<br/>") ;
		out.close() ;
	}
	
	/**
	* @Name: getRequestBodyInfo02
	* @Description: 获取所有的请求参数信息
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 下午4:35:42
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Return: void
	 */
	private void getRequestBodyInfo02(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8") ;
		PrintWriter out = response.getWriter() ;
		Enumeration<String> parameterNames = request.getParameterNames() ;
		while(parameterNames.hasMoreElements()) {
			String parameterName = parameterNames.nextElement() ;
			if("password".equals(parameterName)) {
				String[] parameterValues = request.getParameterValues(parameterName) ;
				out.print(parameterName + ":" + Arrays.toString(parameterValues) + "<br/>") ;
			} else {
				String parameterValue = request.getParameter(parameterName) ;
				out.print(parameterName + ":" + parameterValue + "<br/>") ;
			}
		}
		out.close() ;
	}
	
	/**
	* @Name: setRequestParams2FormBeanByRequest
	* @Description: 将页面请求参数封装到表单JavaBean
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 下午4:49:21
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Return: void
	 */
	private void setRequestParams2FormBeanByRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8") ;
		PrintWriter out = response.getWriter() ;
		Enumeration<String> parameterNames = request.getParameterNames() ;
		Test test = new Test() ;
		while(parameterNames.hasMoreElements()) {
			String parameterName = parameterNames.nextElement() ;
			if("testname".equals(parameterName)) {
				String parameterValue = request.getParameter(parameterName) ;
				test.setUsername(parameterValue) ;
			} 
			if("password".equals(parameterName)) {
				String[] parameterValues = request.getParameterValues(parameterName) ;
				test.setPassword(parameterValues) ;
			}
		}
		out.print(test) ;
		out.close() ;
	}
	
	/**
	* @Name: setRequestParams2FormBeanByPropertyDescriptor
	* @Description: 使用属性描述器PropertyDescriptor,将页面请求参数封装到表单JavaBean
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 下午4:56:03
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Return: void
	 */
	private void setRequestParams2FormBeanByPropertyDescriptor(HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		response.setContentType("text/html;charset=utf-8") ;
		PrintWriter out = response.getWriter() ;
		Enumeration<String> parameterNames = request.getParameterNames() ;
		Test test = new Test() ;
		while(parameterNames.hasMoreElements()) {
			String parameterName = parameterNames.nextElement() ;
			String[] parameterValues = request.getParameterValues(parameterName) ;
			//使用属性描述器PropertyDescriptor,将页面请求参数封装到表单JavaBean
			PropertyDescriptor pd = new PropertyDescriptor(parameterName, Test.class) ;
			Method method = pd.getWriteMethod() ;
			if(parameterValues != null && parameterValues.length > 1) {
				method.invoke(test, (Object)parameterValues) ;
			} else {
				method.invoke(test, parameterValues[0]) ;
			}
		}
		out.print(test) ;
		out.close() ;
	}
	
	/**
	* @Name: setRequestParams2FormBeanByBeanUtils
	* @Description: 使用BeanUtils插件,实现将页面请求参数封装到表单JavaBean
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 下午5:05:18
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws Exception
	* @Return: void
	 */
	private void setRequestParams2FormBeanByBeanUtils(HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		response.setContentType("text/html;charset=utf-8") ;
		PrintWriter out = response.getWriter() ;
		Test test = new Test() ;
		Map<String, String[]> parameterMap = request.getParameterMap() ;
		//使用BeanUtils插件,实现将页面请求参数封装到表单JavaBean
		//使用BeanUtils.copyProperty(bean, name, value)方法
//		for(Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
//			String paramName = entry.getKey() ;
//			String[] paramValue = entry.getValue() ;
//			//使用BeanUtils插件,实现将页面请求参数封装到表单JavaBean
//			BeanUtils.copyProperty(test, paramName, paramValue) ;
//		}
		//使用BeanUtils.populate(bean, properties)方法
		BeanUtils.populate(test, parameterMap) ;
		out.print(test) ;
		out.close() ;
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
